package com.lh.java.basic.threadlocal;

import com.sun.org.apache.bcel.internal.generic.NEW;


public class TestSyncronizedThread {

	public static void main(String[] args) {		
//        Thread1 t1 = new Thread1(); 
//        Thread ta = new Thread(t1, "A"); 
//        Thread tb = new Thread(t1, "B"); 
//        ta.start(); 
//        tb.start();  
//        ObjectOne ob = new ObjectOne();
//		Thread2 t2 = new Thread2(ob);
//		Thread3 t3 = new Thread3(ob);
//		  Thread ta = new Thread(t2, "Thread2"); 
//	       Thread tb = new Thread(t3, "Thread3"); 
//	        ta.start(); 
//	        tb.start(); 
		
		   ObjectOne2 ob2 = new ObjectOne2();
			Thread5 t2 = new Thread5(ob2);
			Thread6 t3 = new Thread6(ob2);
			  Thread ta = new Thread(t2, "Thread2"); 
		       Thread tb = new Thread(t3, "Thread3"); 
		        ta.start(); 
		        tb.start(); 
		
	}

}
class Thread1 implements Runnable { 
    public void run() { 
         synchronized(this) {         	
              for (int i = 0; i < 5; i++) { 
            	  try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
                   System.out.println(Thread.currentThread().getName() + " synchronized loop " + i); 
              } 
         } 
    }  
}

class ObjectOne{
	public int i=2; 
	
	
	public  void add(){		
			i+=1;
		
		
	}
	
	public  void desc(){
		i-=1;		
	}
	
}

class ObjectOne2{
	public int i=4; 
	
	
	public  void add(){		
			i+=1;		
		
	}
	
	public   void desc(){
		i-=1;		
	}
	
}

class Thread2 implements Runnable{
	private ObjectOne ob =null;
	Thread2(ObjectOne ob){
		this.ob = ob;
	}
	@Override
	public void run() {
		synchronized(this){
		for(int i=0;i<10;i++){
			this.ob.add();
			  try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	             System.out.println(Thread.currentThread().getName() + "  i=" + this.ob.i); 	
		}
		}
	}
	
}

class Thread3 implements Runnable{
	private ObjectOne ob =null;
	Thread3(ObjectOne ob){
		this.ob = ob;
	}
	@Override
	public void run() {
		synchronized(this){
		for(int i=0;i<10;i++){
			this.ob.desc();
			  try {
					Thread.sleep(600);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	             System.out.println(Thread.currentThread().getName() + "  i=" + this.ob.i); 	
		}
		}
		
	}
	
}

class Thread5 implements Runnable{
	private ObjectOne2 ob =null;
	Thread5(ObjectOne2 ob){
		this.ob = ob;
	}
	@Override
	public void run() {	
		synchronized(ob){
		for(int i=0;i<10;i++){
			this.ob.add();
			  try {
					Thread.sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	             System.out.println(Thread.currentThread().getName() + "  i=" + this.ob.i); 	
		}
		}
	}
	
}

class Thread6 implements Runnable{
	private ObjectOne2 ob =null;
	Thread6(ObjectOne2 ob){
		this.ob = ob;
	}
	@Override
	public void run() {		
		synchronized(ob){
		for(int i=0;i<10;i++){
			this.ob.desc();
			  try {
					Thread.sleep(600);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	             System.out.println(Thread.currentThread().getName() + "  i=" + this.ob.i); 	
		}
		}
		
	}
	
}